/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package models;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import tableModels.TableReservationModel;
import utils.Connexion;
import utils.TableModel;

/**
 *
 * @author Betty
 */
public class ReservationMapper {
    private Connexion _connexion;

    
    public ReservationMapper() {
        try {
            this._connexion = new Connexion();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ReservationMapper.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(ReservationMapper.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public TableReservationModel fetchAll() {
        String requete = "SELECT reservation.numcommande, reservation.datecommande,client.nom, circuit.identifiant, reservation.nbrplace FROM projet.reservation INNER JOIN projet.client ON reservation.id_client=client.id_client INNER JOIN projet.Circuit ON reservation.identifiant=circuit.identifiant";
        String[] titre = {"Numéro de commande","Date de Commande","Client","ID Circuit", "Place" };
        TableReservationModel model = null;
        try {
            model = new TableReservationModel(this._connexion,requete,titre);
            model.getRowCount();
        } catch (SQLException ex) {
            Logger.getLogger(ReservationMapper.class.getName()).log(Level.SEVERE, null, ex);
        }
            return model;
    }
    
    public void update(Reservation reservation) {
        try {
            String requete = "IDENTIFIANT="
                    + "UPDATE PROJET.RESERVATION SET DATECOMMANDE='" + reservation.getDatecommande() + "',"
                    + "ID_CLIENT=" + reservation.getId_client()+"," 
                    + "IDENTIFIANT=" + reservation.getIdentifiant()+ ","
                    + "NBRPLACE=" + reservation.getNbrplace();
            this._connexion.setUpdate(requete);
        } catch (SQLException ex) {
            Logger.getLogger(ReservationMapper.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
 public Reservation find(int id, Reservation reservation) throws SQLException  {
        String requete = "SELECT DATECOMMANDE, ID_CLIENT, IDENTIFIANT, NBRPLACE FROM PROJET.RESERVATION WHERE NUMCOMMANDE=" + id;
        this._connexion.setQuery(requete);
        ResultSet resultat = this._connexion.getResultSet();
        while(resultat.next()){
            reservation.setNumcommande(Integer.parseInt(resultat.getString("NUMCOMMANDE")));
            reservation.setDatecommande(resultat.getString("DATECOMMANDE"));
            reservation.setId_client(Integer.parseInt(resultat.getString("ID_CLIENT")));
            reservation.setIdentifiant(Integer.parseInt(resultat.getString("IDENTIFIANT")));
            reservation.setNbrplace(Integer.parseInt(resultat.getString("NBRPLACE")));
            
        }
        return reservation;
    }
   
    public void save(Reservation reservation) {
        if(reservation.getNumcommande()== 0){
            this.add(reservation);
        } else {
           this.update(reservation);
        }
    }
    
    public void add(Reservation reservation) {
        try {
            String requete = "INSERT INTO PROJET.RESERVATION VALUES(0,'" + reservation.getDatecommande() + "',"
                     + reservation.getId_client() + "',"
                    + "'" + reservation.getIdentifiant() + "','" + reservation.getNbrplace() + "')";
            this._connexion.setUpdate(requete);
        } catch (SQLException ex) {
            Logger.getLogger(ReservationMapper.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}